package com.imyuanma.qingyun.lowcode.controller;

import com.imyuanma.qingyun.common.model.request.WebRequest;
import com.imyuanma.qingyun.common.model.response.Page;
import com.imyuanma.qingyun.common.model.PageQuery;
import com.imyuanma.qingyun.common.model.response.Result;
import com.imyuanma.qingyun.common.util.AssertUtil;
import com.imyuanma.qingyun.interfaces.common.model.vo.KeyValueVO;
import com.imyuanma.qingyun.lowcode.model.bo.GenerateMainBO;
import com.imyuanma.qingyun.lowcode.model.enums.*;
import com.imyuanma.qingyun.lowcode.model.vo.LcpGenerateCodeVO;
import com.imyuanma.qingyun.lowcode.service.ILcpGenerateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 代码生成web
 *
 * @author wangjy
 * @date 2022/05/04 16:12:37
 */
@RestController
@RequestMapping(value = "/lowcode/generate", method = {RequestMethod.POST})
public class LcpGenerateController {
    /**
     * 代码生成服务
     */
    @Autowired
    private ILcpGenerateService lcpGenerateService;

    /**
     * 生成代码
     * @param webRequest
     * @return
     */
    @RequestMapping(value = "/generateCode")
    public Result generate(@RequestBody WebRequest<LcpGenerateCodeVO> webRequest) {
        LcpGenerateCodeVO lcpGenerateCodeVO = webRequest.getBody();
        AssertUtil.notNull(lcpGenerateCodeVO, "参数无效");
        AssertUtil.notNull(lcpGenerateCodeVO.getGenerateId(), "参数无效");
        lcpGenerateService.generate(lcpGenerateCodeVO.getGenerateId(), lcpGenerateCodeVO.getCodeTemplateId());
        return Result.success();
    }

    /**
     * 初始化指定表的代码生成配置
     *
     * @param webRequest
     * @return
     */
    @RequestMapping(value = "/initGenerateByTableName", method = {RequestMethod.POST})
    public Result<GenerateMainBO> initGenerateByTableName(@RequestBody WebRequest<String> webRequest) {
        return Result.success(lcpGenerateService.initGenerateByTableName(webRequest.getBody()));
    }

    /**
     * 新增代码生成配置
     * @param webRequest
     * @return
     */
    @RequestMapping(value = "/addGenerateConfig", method = {RequestMethod.POST})
    public Result addGenerateConfig(@RequestBody WebRequest<GenerateMainBO> webRequest) {
        lcpGenerateService.addGenerateConfig(webRequest.getBody());
        return Result.success();
    }

    /**
     * 更新代码生成配置
     * @param webRequest
     * @return
     */
    @RequestMapping(value = "/update", method = {RequestMethod.POST})
    public Result update(@RequestBody WebRequest<GenerateMainBO> webRequest) {
        lcpGenerateService.update(webRequest.getBody());
        return Result.success();
    }

    /**
     * 查询代码生成详情
     * @param webRequest
     * @return
     */
    @RequestMapping(value = "/getGenerateDetail", method = {RequestMethod.POST})
    public Result<GenerateMainBO> getGenerateDetail(@RequestBody WebRequest<Long> webRequest) {
        if (webRequest.getBody() == null) {
            return Result.error("参数无效");
        }
        return Result.success(lcpGenerateService.getGenerateDetail(webRequest.getBody()));
    }

    /**
     * 同步字段变化
     * @param webRequest
     * @return
     */
    @RequestMapping(value = "/syncFieldChange", method = {RequestMethod.POST})
    public Result<GenerateMainBO> syncFieldChange(@RequestBody WebRequest<Long> webRequest) {
        if (webRequest.getBody() == null) {
            return Result.error("参数无效");
        }
        return Result.success(lcpGenerateService.syncFieldChange(webRequest.getBody()));
    }

//    /**
//     * 查询代码生成列表
//     * @param generateMainBO
//     * @return
//     */
//    @RequestMapping(value = "/getList", method = {RequestMethod.POST})
//    public Result<List<GenerateMainBO>> getList(@RequestBody GenerateMainBO generateMainBO) {
//        return Result.success(lcpGenerateService.getList(generateMainBO));
//    }

    /**
     * 分页查询
     * @param webRequest
     * @return
     */
    @RequestMapping(value = "/getPage", method = {RequestMethod.POST})
    public Page<List<GenerateMainBO>> getList(@RequestBody WebRequest<GenerateMainBO> webRequest) {
        PageQuery pageQuery = webRequest.buildPageQuery();
        return Page.success(lcpGenerateService.getPage(webRequest.getBody(), pageQuery), pageQuery);
    }

    /**
     * 删除
     * @param webRequest
     * @return
     */
    @RequestMapping(value = "/delete", method = {RequestMethod.POST})
    public Result delete(@RequestBody WebRequest<String> webRequest) {
        AssertUtil.notBlank(webRequest.getBody(), "参数无效");
        lcpGenerateService.delete(Arrays.stream(webRequest.getBody().split(",")).map(Long::valueOf).toArray(Long[]::new));
        return Result.success();
    }

    //    @Log("查询字段查询类型")
    @RequestMapping(value = "/getQueryConditionTypeList", method = {RequestMethod.GET, RequestMethod.POST})
    public Result<List<KeyValueVO>> getQueryConditionTypeList() {
        return Result.success(Arrays.stream(ELcpQueryConditionTypeEnum.values()).map(item -> new KeyValueVO(item.getType(), item.getText())).collect(Collectors.toList()));
    }

    //    @Log("查询表格展示格式类型")
    @RequestMapping(value = "/getShowFormatList", method = {RequestMethod.GET, RequestMethod.POST})
    public Result<List<KeyValueVO>> getShowFormatList() {
        return Result.success(Arrays.stream(ELcpShowFormatEnum.values()).map(item -> new KeyValueVO(item.getType(), item.getText())).collect(Collectors.toList()));
    }

    //    @Log("查询表格首列类型")
    @RequestMapping(value = "/getFirstColumnTypeList", method = {RequestMethod.GET, RequestMethod.POST})
    public Result<List<KeyValueVO>> getFirstColumnTypeList() {
        return Result.success(Arrays.stream(ELcpTableColumnTypeEnum.values()).map(item -> new KeyValueVO(item.getType(), item.getText())).collect(Collectors.toList()));
    }

    //    @Log("查询控件类型")
    @RequestMapping(value = "/getControlTypeList", method = {RequestMethod.GET, RequestMethod.POST})
    public Result<List<KeyValueVO>> getControlTypeList() {
        return Result.success(Arrays.stream(ELcpFormControlTypeEnum.values()).map(item -> new KeyValueVO(item.getType(), item.getText())).collect(Collectors.toList()));
    }

    //    @Log("查询表单校验规则列表")
    @RequestMapping(value = "/getControlCheckList", method = {RequestMethod.GET, RequestMethod.POST})
    public Result<List<KeyValueVO>> getControlCheckList() {
        return Result.success(Arrays.stream(ELcpFormControlCheckEnum.values()).map(item -> new KeyValueVO(item.getType(), item.getText())).collect(Collectors.toList()));
    }

    //    @Log("查询服务域名列表")
    @RequestMapping(value = "/getDomainList", method = {RequestMethod.GET, RequestMethod.POST})
    public Result<List<KeyValueVO>> getDomainList() throws Exception {
        List<KeyValueVO> list = new ArrayList<>();
        String[] domainList = {"/", "{contextPath}", "{URL_UMS}", "{URL_FS}", "{URL_CMS}", "{URL_PORTAL}", "{URL_MONITOR}", "{URL_CONFIG}"};
        for (String domain : domainList) {
            list.add(new KeyValueVO(domain, domain));
        }
        return Result.success(list);
    }

    @RequestMapping(value = "/getShowTypeList", method = {RequestMethod.GET, RequestMethod.POST})
    public Result<List<KeyValueVO>> getShowTypeList() {
        return Result.success(Arrays.stream(ELcpFormItemShowTypeEnum.values()).map(item -> new KeyValueVO(item.getType(), item.getText())).collect(Collectors.toList()));
    }

    @RequestMapping(value = "/getDisableTypeList", method = {RequestMethod.GET, RequestMethod.POST})
    public Result<List<KeyValueVO>> getDisableTypeList() {
        return Result.success(Arrays.stream(ELcpFormItemDisableTypeEnum.values()).map(item -> new KeyValueVO(item.getType(), item.getText())).collect(Collectors.toList()));
    }

    @RequestMapping(value = "/getTableRowBtnList", method = {RequestMethod.GET, RequestMethod.POST})
    public Result<List<KeyValueVO>> getTableRowBtnList() {
        return Result.success(Arrays.stream(ELcpTableRowBtnEnum.values()).map(item -> new KeyValueVO(item.getType(), item.getText())).collect(Collectors.toList()));
    }

    @RequestMapping(value = "/getButtonTypeList", method = {RequestMethod.GET, RequestMethod.POST})
    public Result<List<KeyValueVO>> getButtonTypeList() {
        return Result.success(Arrays.stream(ELcpButtonTypeEnum.values()).map(item -> new KeyValueVO(item.getType(), item.getText())).collect(Collectors.toList()));
    }
    @RequestMapping(value = "/getButtonCategoryList", method = {RequestMethod.GET, RequestMethod.POST})
    public Result<List<KeyValueVO>> getButtonCategoryList() {
        return Result.success(Arrays.stream(ELcpButtonCategoryEnum.values()).map(item -> new KeyValueVO(item.getType(), item.getText())).collect(Collectors.toList()));
    }
}
